package com.dks.repository.impl;

import com.dks.dto.DoctorDto;
import com.dks.mapper.DoctorMapper;
import com.dks.mapper.TalksRecordMapper;
import com.dks.models.Doctor;
import com.dks.models.TalksRecord;
import com.dks.repository.IDoctorRepository;
import com.dks.repository.ITalksRecordRepository;
import com.dks.result.ResultModel;
import com.dks.util.Commond;
import com.dks.util.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.dks.util.Commond.MSG_SUCCESS;

@Repository
public class DoctorRepository implements IDoctorRepository {

    @Resource
    private DoctorMapper doctorMapper;


    @Override
    public ResultModel<Boolean> insert(Doctor doctor) {
        ResultModel resultModel = new ResultModel();
        int row = doctorMapper.insert(doctor);
        if (1 == row) {
            resultModel.setCode(Commond.CODE_SUCCESS);
            resultModel.setMessage(MSG_SUCCESS);
            resultModel.setData(row);
        } else {
            resultModel.setCode(Commond.CODE_FAIL);
            resultModel.setMessage(Commond.MSG_FALE);
            resultModel.setData(row);
        }
        return resultModel;
    }

    @Override
    public ResultModel<Boolean> update(Doctor doctor) {
        ResultModel resultModel = new ResultModel();
        int row = doctorMapper.updateByPrimaryKey(doctor);
        if (1 == row) {
            resultModel.setCode(Commond.CODE_SUCCESS);
            resultModel.setMessage(MSG_SUCCESS);
            resultModel.setData(row);
        } else {
            resultModel.setCode(Commond.CODE_FAIL);
            resultModel.setMessage(Commond.MSG_FALE);
            resultModel.setData(row);
        }
        return resultModel;
    }

    @Override
    public ResultModel login(Doctor doctor) {
        ResultModel resultModel = new ResultModel();
        Map map = new HashMap();
        map.put("accountNumber", doctor.getAccountNumber());
        map.put("pwd", doctor.getPwd());
        map.put("departmentId", doctor.getDepartmentId());
        Doctor doctor1 = doctorMapper.selectByDto(map);
        if (null == doctor1) {
            resultModel.setCode(Commond.CODE_FAIL);
            resultModel.setMessage("登录失败,请核对用户名密码");
            resultModel.setData(doctor);
        } else {
            resultModel.setCode(Commond.CODE_SUCCESS);
            resultModel.setMessage(MSG_SUCCESS);
            resultModel.setData(doctor1);
        }
        return resultModel;
    }

    @Override
    public ResultModel<Boolean> reset(Doctor doctor) {
        ResultModel resultModel = new ResultModel();
        //初始密码 123456
        doctor.setPwd("123456");
        int row = doctorMapper.updateByPrimaryKey(doctor);
        if (1 == row) {
            resultModel.setCode(Commond.CODE_SUCCESS);
            resultModel.setMessage(MSG_SUCCESS);
            resultModel.setData(row);
        } else {
            resultModel.setCode(Commond.CODE_FAIL);
            resultModel.setMessage(Commond.MSG_FALE);
            resultModel.setData(row);
        }
        return resultModel;
    }

    @Override
    public ResultModel<Boolean> updatePwd(DoctorDto doctorDto) {
        ResultModel resultModel = new ResultModel();
        if (StringUtils.isEmpty(doctorDto.getOldPwd())) {
            resultModel.setCode(Commond.CODE_FAIL);
            resultModel.setMessage("旧密码不能为空");
        }
        if (StringUtils.isEmpty(doctorDto.getNewPwd())) {
            resultModel.setCode(Commond.CODE_FAIL);
            resultModel.setMessage("新密码不能为空");
        }

        Doctor doctor = new Doctor();
        BeanUtils.copyProperties(doctorDto, doctor);
        doctor.setPwd(doctorDto.getNewPwd());
        int row = doctorMapper.updateByPrimaryKey(doctor);
        if (1 == row) {
            resultModel.setCode(Commond.CODE_SUCCESS);
            resultModel.setMessage(MSG_SUCCESS);
            resultModel.setData(row);
        } else {
            resultModel.setCode(Commond.CODE_FAIL);
            resultModel.setMessage(Commond.MSG_FALE);
            resultModel.setData(row);
        }
        return resultModel;
    }

    @Override
    public ResultModel<Boolean> selectDocterListByDepartmentId(Integer departmentId) {
        ResultModel resultModel = new ResultModel();
        Map map = new HashMap();
        if (null != departmentId) {
            map.put("departmentId", departmentId);
        }
        List<Doctor> doctorList = doctorMapper.getByMapList(map);
        resultModel.setCode(Commond.CODE_SUCCESS);
        resultModel.setMessage(MSG_SUCCESS);
        resultModel.setData(doctorList);
        return resultModel;
    }
}
